package com.buer.storage.api;


import com.buer.storage.business.domain.Area;
import com.buer.storage.business.dto.AreaDto;
import com.buer.storage.business.dto.DepartmentDto;
import com.buer.storage.business.service.AreaService;
import com.buer.storage.common.annotation.Auth;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/area")
public class AreaController {
    @Resource
    private AreaService areaService;

    //增加库区
    @Auth
    @PostMapping("/add.do")
    public BaseResult add(@RequestBody AreaDto areaDto) {
        if (areaService.add(areaDto) > 0) {
            //添加库区成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //添加库区失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //删除库区
    @Auth
    @GetMapping("/delete.do")
    public BaseResult delete(int areaId) {
        if (areaService.delete(areaId) > 0) {
            //删除库区成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除库区失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //修改库区
    @Auth
    @PostMapping("/update.do")
    public BaseResult update(@RequestBody Area area){
        if (areaService.update(area)>0){
            //修改库区成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //修改库区失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //根据id查询库区所有
    @Auth
    @GetMapping("/selectById.do")
    public BaseResult selectById(int areaId){
        Area area = areaService.selectById(areaId);
        return BaseResult.build(ResultEnum.SUCCESS,area);
    }

    //根据库区id查询库位信息
    @Auth
    @GetMapping("/findDepartment.do")
    public BaseResult findDepartment(int areaId){
        List<DepartmentDto> department = areaService.findDepartment(areaId);
        return BaseResult.build(ResultEnum.SUCCESS,department);
    }


}
